package com.itHuangTao.tank.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.itHuangTao.tank.GameType;
import com.itHuangTao.tank.TankApp;
import com.itHuangTao.tank.effects.HelmetEffect;

import static com.almasb.fxgl.dsl.FXGL.spawn;

public class BulletPlayerHandler extends CollisionHandler {
    public BulletPlayerHandler() {
        super(GameType.BULLET, GameType.PLAYER);
    }

    //进行碰撞检测
    @Override
    protected void onCollisionBegin(Entity bullet, Entity player) {
        //判断玩家当前是否有头盔效果
        boolean b = player.getComponent(EffectComponent.class).hasEffect(HelmetEffect.class);
        if(b){
            bullet.removeFromWorld();
        }else {
            HealthIntComponent hp = player.getComponent(HealthIntComponent.class);
            hp.damage(1);//玩家血量减1
            //玩家血量为0的时候
            if(hp.isZero()){
                spawn("explode",player.getCenter().
                        subtract(50/2.0,50/2.0));//在坦克中心爆炸
                player.removeFromWorld();//移除玩家
                //弹出失败场景
                FXGL.<TankApp>getAppCast().gameOver();
            }
            bullet.removeFromWorld();
        }

    }
}
